home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_400
/
436_01
/
stringz.asm
< prev
next >
Wrap
Assembly Source File
|
1994-10-07
|
36KB
|
897 lines
;==========================================================================
; STRINGZ.ASM
;
; C-callable routines that offer string handling capabilities similar
; to those found in BASIC, and then some.
;
; None of the routines in this file allocate memory for, or check for
; overflow in copying to, destination strings. It is the responsibility
; of the calling routine to insure that enough space is available for
; the destination strings, including terminating null bytes.
;
; Assembler: Borland TASM 1.01 (tasm /ml/t/w2/z stringz;)
;
; INCON source files and the object and library files created from
; them are:
; Copyright (c) 1993-94, Richard Zigler.
; You may freely distribute unmodified source, object, and library
; files, and incorporate them into your own non-commercial software,
; provided that this paragraph and the copyright string defined in
; INCON.C are included in all copies.
;==========================================================================
; Function Prototypes
;
; The string pointer and function return types are all declared
; "far", so that these routines can be linked with programs under
; any memory model. The functions in this source file can handle
; strings up to 32,767 bytes long.
;
; char far * pascal far LeftStr ( char far *, char far *, short );
; char far * pascal far RightStr ( char far *, char far *, short );
; char far * pascal far MidStr ( char far *, char far *, short, short );
; char far * pascal far lJust ( char far *, char far *, short, short );
; char far * pascal far rJust ( char far *, char far *, short, short );
; char far * pascal far cJust ( char far *, char far *, short, short );
; char far * pascal far lTrim ( char far *, char far * );
; char far * pascal far rTrim ( char far *, char far * );
; char far * pascal far ReplStr ( char far *, char far *, short, short );
; char far * pascal far InsStr ( char far *, char far *, short, short );
; char far * pascal far TemplStr ( char far *, char far *, short );
;==========================================================================
locals ;enable TASM local symbols
model small, pascal ;set PASCAL stack handling
;--------------------------------------------------------------------------
; Macros to push registers at entry and pop registers at exit.
;--------------------------------------------------------------------------
@PushRegs macro reglist
ifnb <reglist>
irp reg, <reglist>
push reg
endm
endif
endm
@PopRegs macro reglist
ifnb <reglist>
irp reg, <reglist>
pop reg
endm
endif
endm
.code
;--------------------------------------------------------------------------
; LeftStr
;
; Copies Num characters from left end of SrcStr to DestStr; returns
; pointer to DestStr.
;
; If Num is greater than or equal to the length of SrcStr, all of
; SrcStr is copied. If Num is outside the range 1 to 32767, DestStr
; is returned null.
;--------------------------------------------------------------------------
public LEFTSTR
LEFTSTR proc far
arg Num :word ;characters to copy
arg SrcStr :far ptr byte ;source string
arg DestStr :far ptr byte ;destination string
ParmSize = type Num + type SrcStr + type DestStr
@PushRegs <si, di, ds>
mov dx, ParmSize
les di, [DestStr]
call NullPtr
mov dx, es ;save for return
mov bx, di
mov ax, [Num] ;# chars to move
and ax, ax
jle @@Term
lds si, [SrcStr]
call StrLen ;CX = source length
cmp cx, ax
jle @@Move
mov cx, ax
@@Move:
rep movsb
@@Term:
sub al, al
stosb
mov ax, bx ;DX:AX->DestStr
@PopRegs <ds, di, si>
ret
LEFTSTR endp
;--------------------------------------------------------------------------
; RightStr
;
; Copies Num characters from right end of SrcStr to DestStr; returns
; pointer to DestStr.
;
; If Num is greater than or equal to the length of SrcStr, all of
; SrcStr is copied. If Num is outside the range 1 to 32767, DestStr
; is returned null.
;--------------------------------------------------------------------------
public RIGHTSTR
RIGHTSTR proc far
arg Num :word ;characters to copy
arg SrcStr :far ptr byte ;source string
arg DestStr :far ptr byte ;destination string
ParmSize = type Num + type SrcStr + type DestStr
@PushRegs <si, di, ds>
mov dx, ParmSize
les di, [DestStr]
call NullPtr
push es ;save for return
push di
lds si, [SrcStr]
call StrLen ;CX = source length
and cx, cx
jle @@Term ;if out of range
mov bx, cx ;else save in BX
mov dx, cx ; and DX
mov cx, [Num] ;# chars to copy
and cx, cx
jle @@Term ;if Num out of range
sub dx, cx ;else if length > Num
jg @@NumChars ; move Num characters
mov cx, bx ;else move length chrs
jmp short @@Move
@@NumChars:
add si, dx ;start at length - Num
@@Move:
rep movsb
@@Term:
sub al, al
stosb ;plant null terminator
pop ax
pop dx ;DX:AX->DestStr
@PopRegs <ds, di, si>
ret
RIGHTSTR endp
;--------------------------------------------------------------------------
; MidStr
;
; Copies Num characters from SrcStr to DestStr beginning at Start in
; SrcStr; returns pointer to DestStr.
;
; If Start is greater than the length of SrcStr, DestStr is returned
; null. If Num is greater than the remainder of SrcStr, all characters
; from Start to the end of SrcStr are copied. If either Start or Num
; is outside the range 1 to 32767, DestStr is returned null.
;--------------------------------------------------------------------------
public MIDSTR
MIDSTR proc far
arg Num :word ;characters to copy
arg Start :word ;start position
arg SrcStr :far ptr byte ;source string
arg DestStr :far ptr byte ;destination string
ParmSize = type Num + type Start + type SrcStr
ParmSize = ParmSize + type DestStr
@PushRegs <si, di, ds>
mov dx, ParmSize
les di, [DestStr]
call NullPtr
push es ;save for return
push di
mov bx, [Start]
and bx, bx
jle @@Term ;if Start <= 0
mov dx, [Num]
and dx, dx
jle @@Term ;if Num <= 0
lds si, [SrcStr] ;else load source
call StrLen ;CX = source length
and cx, cx
jle @@Term ;if out of range
cmp bx, cx
jg @@Term ;if Start > length